"use strict";
// const ip = "http://59.110.156.72:6099"
const ip = "http://121.37.172.130:6001";
// const ip = "http://182.92.217.252:6093";
const Timestamp = new Date().getTime();
const path = require("path");
const resolve = (dir) => path.join(__dirname, dir);
const compressionWebpackPlugin = require("compression-webpack-plugin");
const BundleAnalyzerPlugin =
  require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const dayjs = require("dayjs");
const version = "1." + dayjs(new Date()).format("YYMM.DD") + ".1";
module.exports = {
  publicPath: "/",
  transpileDependencies: ["*"],
  productionSourceMap: false,
  chainWebpack: (config) => {
    config.optimization.splitChunks({
      chunks: "all",
      cacheGroups: {
        libs: {
          name: "chunk-libs",
          test: /[\\/]node_modules[\\/]/,
          priority: 10,
          chunks: "initial",
        },
        elementUI: {
          name: "chunk-elementUI",
          priority: 20,
          test: /[\\/]node_modules[\\/]_?element-ui(.*)/,
        },
        commons: {
          name: "chunk-commons",
          test: resolve("src/components"),
          minChunks: 3,
          priority: 5,
          reuseExistingChunk: true,
        },
        vendors: {
          name: "vendors",
          test: /[\\/]node_modules[\\/]/,
          priority: -20,
        },
      },
    });
    config.resolve.alias
      .set("@", resolve("src"))
      .set("~", resolve("public"))
      .set("assets", resolve("src/assets"));
    config.plugins.delete("prefetch");
    config.module
      .rule("vue")
      .use("vue-loader")
      .loader("vue-loader")
      .tap((options) => {
        options.compilerOptions.directives = {
          html(node, directiveMeta) {
            (node.props || (node.props = [])).push({
              name: "innerHTML",
              value: `$xss(_s(${directiveMeta.value}))`,
            });
          },
        };
        return options;
      });
    config.plugin("define").tap((args) => {
      args[0]["process.env"].version = JSON.stringify(version);
      return args;
    });
    config.when(process.env.ANALYZER == "true", (config) => {
      config.plugin("webpack-bundle-analyzer").use(BundleAnalyzerPlugin);
    });
  },
  configureWebpack: (config) => {
    if (process.env.NODE_ENV === "production") {
      return {
        plugins: [
          new compressionWebpackPlugin({
            algorithm: "gzip",
            test: /\.(js|css)$/, // 匹配文件名
            threshold: 10240, // 对超过10k的数据压缩
            deleteOriginalAssets: false, // 不删除源文件
            minRatio: 0.8, // 压缩比
          }),
        ],
        output: {
          filename: `js/[name].${Timestamp}.js`,
          chunkFilename: `js/[name].${Timestamp}.js`,
        },
      };
    }
  },
  devServer: {
    open: true,
    https: false,
    hotOnly: false,
    port: 9000,
    disableHostCheck: true,
    proxy: {
      // 配置跨域
      "/api": {
        target: ip,
        ws: false,
        changOrigin: true,
        pathRewrite: {
          "^/api": "",
        },
      },
    },
  },
  css: {
    // 全局样式
    loaderOptions: {
      sass: {
        prependData: `@import "@/styles/variable";`,
      },
      // postcss: {
      //   plugins: [postcss],
      // },
      postcss: {
        config: {
          path: "./postcss.config.js",
        },
      },
    },
  },
};

let text = require("./public/version.json") || [];

const fs = require("fs");
const content = "轨迹文件上传,点击故障管理跳转外部网站";
if (process.env.NODE_ENV === "production") {
  text = {
    version,
    content,
    updateTime: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss"),
  };
  fs.writeFile("./public/version.json", JSON.stringify(text), () => {
    console.log(content);
  });
}
